package com.hm.ibsm.window.main;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.apache.http.HttpResponse;

import com.alibaba.fastjson.TypeReference;
import com.hm.common.network.httpclient.HttpClientFactory;
import com.hm.common.network.httpclient.HttpClientResponseParse;
import com.hm.common.su.bean.ServerResponse;
import com.hm.ibsm.manage.vo.auth.UserMenuVo;
import com.hm.ibsm.su.GlobalContent;
import com.hm.ibsm.su.IbsmServerResponseParse;
import com.hm.ibsm.su.LocationConfig;
import com.hm.ibsm.window.MainWindow;

/**
 * @author shishun.wang
 * @date 2017年2月18日 下午6:58:24
 * @version 1.0
 * @describe
 */
public class LeftMenuPanel extends JPanel {

	private static final long serialVersionUID = -4330393789215033180L;

	public static String USER_MENU_URI = "/rest/ibsm/mg/v1.0/auth/user/menus";

	public LeftMenuPanel(MainWindow mainWindow, final ContentPanel contentPanel) {
		this.setBorder(new LineBorder(new Color(211, 211, 211), 1));
		this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));

		JTree tree = new JTree();
		{// 加载目录
			UserMenuVo menu = new UserMenuVo();
			{// 根目录
				menu.setMenuId("-1");
				menu.setMenuName("根目录");
			}
			DefaultMutableTreeNode root = new DefaultMutableTreeNode(menu);
			loadUserMenus(root);// 初始化菜单数据
			DefaultTreeModel model = new DefaultTreeModel(root);
			tree.setModel(model);
			this.expandAll(tree, new TreePath((TreeNode) tree.getModel().getRoot()), true);
		}

		tree.setCellRenderer(new DefaultTreeCellRenderer() {

			private static final long serialVersionUID = -4058476594951687486L;

			@Override
			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean select, boolean expanded, boolean leaf, int row,
					boolean hasFocus) {
				super.getTreeCellRendererComponent(tree, value, select, expanded, leaf, row, hasFocus);

				DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
				UserMenuVo menu = (UserMenuVo) node.getUserObject();
				this.setText(menu.getMenuName());
				return this;
			}
		});

		tree.setRootVisible(false);
		tree.getSelectionModel().setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
			public void valueChanged(javax.swing.event.TreeSelectionEvent event) {
				JTree tree = (JTree) event.getSource();
				DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				if (selectNode.isLeaf()) {
					UserMenuVo menu = (UserMenuVo) selectNode.getUserObject();
					GlobalContent.show(menu.getMenuId(), contentPanel);
				}
			}
		});
		tree.setSelectionRow(0);
		this.add(tree);
	}

	private void expandAll(JTree tree, TreePath parent, boolean expand) {
		TreeNode node = (TreeNode) parent.getLastPathComponent();
		if (node.getChildCount() >= 0) {
			for (@SuppressWarnings("rawtypes")
			Enumeration e = node.children(); e.hasMoreElements();) {
				TreeNode n = (TreeNode) e.nextElement();
				TreePath path = parent.pathByAddingChild(n);
				expandAll(tree, path, expand);
			}
		}
		if (expand) {
			tree.expandPath(parent);
		} else {
			tree.collapsePath(parent);
		}
	}

	private void loadUserMenus(DefaultMutableTreeNode root) {
		try {
			HttpResponse response = LocationConfig.accredit(HttpClientFactory.PATCH.build(LocationConfig.baseUri(USER_MENU_URI))).execute();
			List<UserMenuVo> menus = IbsmServerResponseParse
					.parse(HttpClientResponseParse.parseGeneric(response, new TypeReference<ServerResponse<List<UserMenuVo>>>() {
					}));
			initTree(menus, root);
		} catch (Exception e) {

		}
	}

	private void initTree(List<UserMenuVo> menus, DefaultMutableTreeNode parent) {
		for (UserMenuVo menu : menus) {
			DefaultMutableTreeNode curentMenu = new DefaultMutableTreeNode(menu);
			parent.add(curentMenu);
			if (menu.isHasChild()) {
				initTree(menu.getChilds(), curentMenu);
			}
		}
	}

}
